﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Drawing;
using System.IO;
using System.Threading;
using System.Text;
using Business;
using System.Web.Security;
using DataAccess.i2iPortalTableAdapters;

public partial class admin_View_Machine : @base
{
	Encryption objenc = new Encryption();
	ImageEditor objimg = new ImageEditor();
	protected void Page_Load(object sender, EventArgs e)
	{

	}
	/// <summary>
	/// Handles the Click event of the UpdateButton control.
	/// If there is no user input 
	/// Set the text for photoTextBox to "NotAvailable.jpg"
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void UpdateButton_Click(object sender, EventArgs e)
	{
		Label photoLabel = FormViewMachines.FindControl("photoLabel") as Label;
		if (photoLabel.Text == string.Empty || photoLabel.Text == "" || photoLabel.Text == "&nbsp;" || photoLabel.Text == "Not Available")
		{
			photoLabel.Text = "NotAvailable.jpg";
		}
	}
	/// <summary>
	/// Handles the SelectedIndexChanged event of the GridViewComponents control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	/// <action>go to View_Comp.aspx with component_id as query string</action>
	protected void GridViewComponents_SelectedIndexChanged(object sender, EventArgs e)
	{
		string component_id = GridViewComponents.SelectedDataKey.Value.ToString();
		int ComponentID = 0;
		Int32.TryParse(component_id, out ComponentID);
		ClientForComponentTableAdapter adapter = new ClientForComponentTableAdapter();
		var clientTable = adapter.GetClientIDByComponentID(ComponentID);
		var clientID = clientTable.Rows[0]["client_id"];
		Session["CompanyID"] = clientID;
		Server.Transfer("View_Comp.aspx?component_id=" + component_id);
	}
	/// <summary>
	/// Handles the Click event of the CreateComponentButton control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	/// <action>go to Create_Comp.aspx with machine_id as query string</action>
	protected void CreateComponentButton_Click(object sender, EventArgs e)
	{
		string machine_id = objenc.EncodeUrl(FormViewMachines.SelectedValue.ToString());
		Server.Transfer("Create_Comp.aspx?machine_id=" + machine_id);
	}
	/// <summary>
	/// Handles the SelectedIndexChanged event of the GridViewWorkOrders control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	/// <action>go to View_WO.aspx with machine_id and workorder_id as query string</action>
	protected void GridViewWorkOrders_SelectedIndexChanged(object sender, EventArgs e)
	{
		string workorder_id = GridViewWorkOrders.SelectedDataKey.Value.ToString();
		string machine_id = FormViewMachines.SelectedValue.ToString();
		Server.Transfer("View_WO.aspx?machine_id=" + machine_id + "&workorder_id=" + workorder_id);
	}
	/// <summary>
	/// Handles the RowDataBound event of the GridViewWorkOrders control.
	/// If there is no data in the "Action" column of GridViewWorkOrders
	/// Set the row of that column to "N/A"
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.Web.UI.WebControls.GridViewRowEventArgs"/> instance containing the event data.</param>
	protected void GridViewWorkOrders_RowDataBound(object sender, GridViewRowEventArgs e)
	{
		if (e.Row.RowType == DataControlRowType.DataRow)
		{
			if (e.Row.Cells[2].Text == "&nbsp;" || e.Row.Cells[2].Text == "")
			{
				e.Row.Cells[2].Text = "N/A";
			}
		}
	}
	/// <summary>
	/// Handles the DataBound event of the FormViewMachines control.
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void FormViewMachines_DataBound(object sender, EventArgs e)
	{
		Label photoLabel = FormViewMachines.FindControl("photoLabel") as Label;
		System.Web.UI.WebControls.Image photoImage = FormViewMachines.FindControl("photoImage") as System.Web.UI.WebControls.Image;
		//setting the Machine Image
		string path = Server.MapPath("../photos/" + photoLabel.Text);

		System.Drawing.Image image = System.Drawing.Image.FromFile(path, true);
		ImageEditor objimg = new ImageEditor();
		int height = 0;
		int width = 0;
		if (objimg.isPortrait(image))
		{
			height = 160;
			width = 120;
		}
		else
		{
			height = 120;
			width = 160;
		}
		photoImage.Attributes.Add("Width", Convert.ToString(width));
		photoImage.Attributes.Add("Height", Convert.ToString(height));
		photoImage.ImageUrl = "../photos/" + photoLabel.Text;

		if (FormViewMachines.CurrentMode == FormViewMode.Edit)
		{
			if (photoLabel.Text != "NotAvailable.JPG")
			{
				Button removeButton = FormViewMachines.FindControl("removeButton") as Button;
				removeButton.Visible = true;
			}
		}
	}
	/// <summary>
	/// Handles the Click event of the uploadButton control.
	/// Resize the original image to certain size
	/// Save the image to the path folder
	/// </summary>
	/// <param name="sender">The source of the event.</param>
	/// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
	protected void uploadButton_Click(object sender, EventArgs e)
	{
		Label photoLabel = FormViewMachines.FindControl("photoLabel") as Label;
		FileUpload photoFileUpload = FormViewMachines.FindControl("photoFileUpload") as FileUpload;
		Label statusLabel = FormViewMachines.FindControl("statusLabel") as Label;
		System.Web.UI.WebControls.Image photoImage = FormViewMachines.FindControl("photoImage") as System.Web.UI.WebControls.Image;

		//to retrieve only Filename from the complete path
		int extractPos = photoFileUpload.FileName.LastIndexOf("\\") + 1;
		string photoFileName = photoFileUpload.FileName.Substring(extractPos, photoFileUpload.FileName.Length - extractPos);
		if (photoFileName.IndexOf("/") >= 0 || photoFileName.IndexOf("\\") >= 0)
		{
			//We found a / or \
			Response.End();
		}

		if (photoFileUpload.HasFile)
		{
			//to check file extension is JPG.
			string fileExt = System.IO.Path.GetExtension(photoFileUpload.FileName);
			if (fileExt.ToUpper() == ".JPG")
			{

				//resize image before uploading - start.
				try
				{
					System.Drawing.Image imageToBeResized = System.Drawing.Image.FromStream(photoFileUpload.PostedFile.InputStream);
					int height = 0;
					int width = 0;
					// check if image is in potrait or landscape scale.
					if (objimg.isPortrait(imageToBeResized))
					{
						height = 800;
						width = 600;
					}
					else
					{
						height = 600;
						width = 800;
					}

					Bitmap bitmap = objimg.ResizeThisImage(width, height, imageToBeResized);
					//Save uploaded file to server at the in the Pics folder.
					bitmap.Save(Request.PhysicalApplicationPath
									+ "photos\\" + photoFileName, ImageFormat.Jpeg);
					statusLabel.Text = " File Name: " + photoFileUpload.PostedFile.FileName + "<br>" +
							" File Size: " + photoFileUpload.PostedFile.ContentLength + " kb<br>" +
							" File Type: " + photoFileUpload.PostedFile.ContentType;
					photoImage.Visible = true;
					photoImage.Attributes.Add("Width", Convert.ToString(0.4 * bitmap.Width));
					photoImage.Attributes.Add("Height", Convert.ToString(0.4 * bitmap.Height));
					photoImage.ImageUrl = "../photos/" + photoFileName;

					bitmap.Dispose();
					imageToBeResized.Dispose();

					//bind photo file name to textbox.
					photoLabel.Text = photoFileName;
					Button removeButton = FormViewMachines.FindControl("removeButton") as Button;
					removeButton.Visible = true;

				}
				catch (ArgumentException)
				{
					statusLabel.Text = "Unknown image file!";
				}
				catch (ThreadAbortException)
				{

				}
				catch (Exception ex)
				{
					statusLabel.Text = "Unknown error: " + ex;
				}
			}
			else
			{
				statusLabel.Text = "Only 'jpeg' files allowed!";
			}
		}
		else
		{
			statusLabel.Text = "You have not specified a file.";
		}
	}
	protected void removeButton_Click(object sender, EventArgs e)
	{
		Label photoLabel = FormViewMachines.FindControl("photoLabel") as Label;
		System.Web.UI.WebControls.Image photoImage = FormViewMachines.FindControl("photoImage") as System.Web.UI.WebControls.Image;
		photoLabel.Text = "Not Available";
		photoImage.Visible = false;
	}
	protected void getDataSheet_Click(object sender, EventArgs e)
	{
		string machine_id = FormViewMachines.SelectedValue.ToString();
		// TextBox machine_name = FormViewMachines.FindControl("machineNameTextBox") as TextBox;
		MACHINEID = ToInt(machine_id);
		// MACHINENAME = machine_name.Text;
		Server.Transfer("TechnicalDataSheet.aspx");
	}
	protected void LoginStatus1_LoggingOut(object sender, LoginCancelEventArgs e)
	{


		Session.Contents.Remove("UserDetails");
		Session.Abandon();
		Session.Contents.RemoveAll();
		FormsAuthentication.SignOut();
		Response.Redirect("../Default.aspx");
	}

}
